package com.css.dzj.dzjwgr.modules.updown.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjwgr.common.util.R;
import com.css.dzj.dzjwgr.modules.updown.entity.CommAttachmentEntity;
import com.css.dzj.dzjwgr.modules.updown.service.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ud")
public class UpdAndDownController {
    @Autowired
    private AttachService attachService;

    //项目模块下所有附件目录
    @RequestMapping("/selectId")
    public R selectAllId(String id) {
        List<CommAttachmentEntity> attachmentEntities = attachService.queryAll(id);
        if (attachmentEntities != null && attachmentEntities.size() > 0) {
            return R.ok().put("data", attachmentEntities);
        }
        return R.error("附件未上传");
    }

    @RequestMapping("/save")
    public R save(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "id") String id) {
        try {
            String s = attachService.save(file, id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传错误");
        }
        return R.ok().put("data", "ok");
    }

    @RequestMapping("/download")
    public String download(String id) {
        try {
            attachService.download(id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("下载出现错误").toString();
        }
        return R.ok().put("data", "ok").toString();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R del(String id) {
        String resultInfo = attachService.del(id);
        if("ok".equals(resultInfo)){
            return R.ok().put("data", resultInfo);
        }else{
            return R.error("删除失败");
        }

    }
    //项目模块下所有附件目录(外部接口)
    @RequestMapping("/selectOneId")
    public List<CommAttachmentEntity> selectOneId(String id) {
        List<CommAttachmentEntity> attachmentEntities = attachService.queryAll(id);
        if (attachmentEntities != null && attachmentEntities.size() > 0) {
            return attachmentEntities;
        }
        return null;
    }
    /**
     *接收门户系统传来的投诉举报附件实体
     * @author zch
     * @date 2020\3\20 0011
     */
    @PostMapping("/saveAttachVO")
    public R enterprise( @RequestBody CommAttachmentEntity itemVO) {
        try {
            if(StringUtils.isEmpty(itemVO)){
               // baseService.logFailed(3, "操作失败", "2");
                return R.error("参数为空");
            }
           /* CommAttachmentEntity commAttachmentEntity = new CommAttachmentEntity();
            CommAttachmentEntity item = new CommAttachmentEntity();
            commAttachmentEntity.setFileurl((String)((JSONObject) JSONObject).get("fileurl"));
            commAttachmentEntity.setTableName((String)((JSONObject) JSONObject).get("tableName"));
            commAttachmentEntity.setFilename((String)((JSONObject) JSONObject).get("filename"));
            commAttachmentEntity.setFileext((String)((JSONObject) JSONObject).get("fileext"));
            commAttachmentEntity.setTableUuid((String)((JSONObject) JSONObject).get("tableUuid"));
            commAttachmentEntity.setFilesize((Integer) ((JSONObject) JSONObject).get("filesize"));*/
            attachService.addAttach(itemVO);
            //baseService.logSuccess(3, "操作成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
           // baseService.logFailed(3, "操作失败", "2");
            return R.error(e.getMessage());
        }
    }
}
